package com.wxxymaker.edu.controller;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.CourseSpecialty;
import com.wxxymaker.edu.model.entity.CourseSubject;
import com.wxxymaker.edu.roles.IgnoreAuth;
import com.wxxymaker.edu.roles.TeacherRoles;
import com.wxxymaker.edu.service.CourseService;
import com.wxxymaker.edu.service.CourseSpecialtyService;
import com.wxxymaker.edu.service.CourseSubjectService;
import com.wxxymaker.edu.utils.TimeUtils;
import io.github.biezhi.anima.page.Page;

import java.util.*;

/**
 * @AUTHOR soft
 * @DATE 2018/10/16 17:05
 * @DESCRIBE 课程类型处理器 年级specialty 专业subject
 */
@TeacherRoles
@Path(value = "/course/type", restful = true)
public class CourseTypesHandler {

    @Inject
    private CourseSubjectService courseSubjectService;
    @Inject
    private CourseSpecialtyService courseSpecialtyService;
    @Inject
    private CourseService courseService;

    @IgnoreAuth
    @PostRoute("/subject/page")
    public RestResponse pageSubject(@Param Integer csid,
                                    @Param(defaultValue = "1") int page,
                                    @Param(defaultValue = "10", name = "limit") int size,
                                    @Param(defaultValue = "false") Boolean nr) {
        Page<CourseSubject> page1 = courseSubjectService.page(csid, page, size);
        if (page1 != null) {
            if (nr) {
                List<CourseSubject> rows = page1.getRows();
                Set<CourseSubject> subjects = new TreeSet<CourseSubject>(Comparator.comparing(CourseSubject::getName));
                subjects.addAll(rows);
                List<CourseSubject> collect = new ArrayList<>(subjects);
                page1.setRows(collect);
            }
            return RestResponse.ok(page1);
        }
        return RestResponse.fail();
    }

    @PostRoute("/subject/add")
    public RestResponse addSubject(CourseSubject subject) {
        Page<CourseSpecialty> courseSpecialtyPage = courseSpecialtyService.page(1,10);
        subject.setTime(TimeUtils.getTime());
        CourseSubject add = courseSubjectService.add(subject);
        if (add != null && courseSpecialtyPage != null) {
            return RestResponse.ok("课程添加成功！");
        }else if (courseSpecialtyPage == null) {
            return RestResponse.fail("暂无年级,请先添加年级!");
        }
        return RestResponse.fail();
    }

    @PostRoute("/subject/delete/:id")
    public RestResponse deleteSubject(@PathParam Integer id) {
        CourseSubject delete = courseSubjectService.delete(id);
        if (delete != null) {
            return RestResponse.ok(delete);
        }
        return RestResponse.fail();
    }

    @PostRoute("/subject/delete")
    public RestResponse AdminDeleteSubject(@Param Integer subjectId) {
        CourseSubject deleteSubject = null;
        Boolean have = courseService.have(subjectId);
        if (!have) {
            deleteSubject = courseSubjectService.delete(subjectId);
            return RestResponse.ok(deleteSubject);
        }
        return RestResponse.fail();
    }

    @PostRoute("/subject/change")
    public RestResponse changeSubject(@Param Integer specialtyId, @Param String name, @Param Integer id) {
        CourseSubject courseSubject = courseSubjectService.byId(id);
        if (courseSubject != null) {
            courseSubject.setSpecialtyId(specialtyId);
            courseSubject.setName(name);
            courseSubject.update();
            return RestResponse.ok(id);
        }
        return RestResponse.fail();
    }

    @IgnoreAuth
    @PostRoute("/specialty/page")
    public RestResponse pageSpecialty(@Param(defaultValue = "1") int page,
                                      @Param(defaultValue = "10", name = "limit") int size) {
        Page<CourseSpecialty> page1 = courseSpecialtyService.page(page, size);
        if (page1 != null) {
            return RestResponse.ok(page1);
        }
        return RestResponse.fail();
    }

    @PostRoute("/specialty/add")
    public RestResponse addSpecialty(CourseSpecialty specialty) {
        specialty.setTime(TimeUtils.getTime());
        CourseSpecialty add = courseSpecialtyService.add(specialty);
        if (add != null) {
            return RestResponse.ok("年级添加成功!");
        }
        return RestResponse.fail("年级添加失败!");
    }

    @PostRoute("/specialty/delete/:id")
    public RestResponse deleteSpecialty(@PathParam Integer id) {
        CourseSpecialty delete = courseSpecialtyService.delete(id);
        if (delete != null) {
            return RestResponse.ok(delete);
        }
        return RestResponse.fail();
    }

    @PostRoute("/specialty/delete")
    public RestResponse AdmineleteSpecialty(@Param Integer specialtyId) {
        Boolean deleteSpecialty = false;
        if (courseSubjectService.count(specialtyId) == 0) {
            deleteSpecialty = courseSpecialtyService.cancel(specialtyId);
            return RestResponse.ok(deleteSpecialty);
        } else
            return RestResponse.fail("该年级内还有课程！不可以删除！");
    }


    @PostRoute("/specialty/change")
    public RestResponse changeSpecialty(@Param String name,
                                        @Param Integer id) {
        CourseSpecialty courseSpecialty = courseSpecialtyService.byId(id);
        if (courseSpecialty != null) {
//            courseSpecialty.setId(id);
            courseSpecialty.setName(name);
            courseSpecialty.update();
            return RestResponse.ok(courseSpecialty);
        }
        return RestResponse.fail();
    }

    @IgnoreAuth
    @PostRoute("/subject/sp_page")
    public RestResponse pagesubject_sp(@Param int specialtyId,
                                       @Param(defaultValue = "1") int page,
                                       @Param(defaultValue = "10", name = "limit") int size) {
        Page<CourseSubject> page1 = courseSubjectService.sp_page(specialtyId, page, size);
        if (page1 != null) {
            return RestResponse.ok(page1);
        }
        return RestResponse.fail();
    }

//    @IgnoreAuth
//    @PostRoute("/specialty/sp_page")
//    public RestResponse pagespecialty_sp(@Param int subjectId,
//                                       @Param(defaultValue = "1") int page,
//                                       @Param(defaultValue = "10") int size) {
//        Page<CourseSpecialty> page1 = courseSpecialtyService.sp_page(subjectId, page, size);
//        if (page1 != null) {
//            return RestResponse.ok(page1);
//        }
//        return RestResponse.fail();
//    }
}
